package cn.sinobest.pe;

import java.sql.DriverManager;

import org.pentaho.di.core.Const;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.logging.KettleLogStore;
import org.pentaho.di.core.logging.log4j.Log4jLogging;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.hnisi.framework.util.Configuration;

public class ExcutorFactory {
	
	/**
	 * 
	 */
	private final static Logger LOG = LoggerFactory.getLogger(ExcutorFactory.class);
	
	public static Excutor create(Configuration config) throws Exception {
		System.setProperty("KETTLE_PLUGIN_BASE_FOLDERS", (new java.io.File("")).getAbsolutePath()+"/plugins,"+Const.DEFAULT_PLUGIN_BASE_FOLDERS);
		config.setValue("workerId", System.getProperty("KETTLE_SYSTEM_HOSTNAME"));
		config.setValue("workerRpcPort", System.getProperty("workerRpcPort"));
		
		/**
		 * 初始kettle的carte
		 */
		try {
			DriverManager.setLoginTimeout(2*60*60);
			KettleEnvironment.init();
			KettleLogStore.getAppender().addLoggingEventListener(new Log4jLogging());
		} catch (KettleException e1) {
			LOG.error("kettle enviro inited failed!", e1);
			return null;
		}
		
		return new Excutor(config);
	}
}
